package com.gary.DBCP;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;

public class DBCP {
	private static BasicDataSource dataSource = null;

	public DBCP() {
		init();
	}

	public static void init() {
		if (dataSource != null) {
			try {
				dataSource.close();
			} catch (Exception e) {
			}
			dataSource = null;
		}
		try {
			Properties p = new Properties();
			p.setProperty("driverClassName", "com.mysql.jdbc.Driver");
			p.setProperty("url",
					"jdbc:mysql://localhost:3306/breaks?" +
					"useUnicode=true&amp;characterEncoding=GBK");
			p.setProperty("password","");
			p.setProperty("username", "root");
			p.setProperty("maxActive", "100");
			p.setProperty("maxIdle", "50");
			p.setProperty("minIdle", "30");
			p.setProperty("initialSize", "30");
			dataSource = (BasicDataSource) BasicDataSourceFactory.createDataSource(p);
		} catch (Exception e) {
		}
	}

	public static synchronized Connection getConnection() throws SQLException {
		return dataSource.getConnection();
	}

	public static void main(String args[]) {
		DBCP dbcp = new DBCP();
		Connection conn = null;
		Statement sta = null;
		ResultSet res = null;
		try {
			conn = dbcp.getConnection();
			sta = conn.createStatement();
			res=sta.executeQuery("select * from user");
			while (res.next()) {
				System.out.println(res.getString("user_name"));
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
